package com.lin.lazybone.adapter;

import android.animation.ObjectAnimator;
import android.content.ContentValues;
import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lin.lazybone.R;
import com.lin.lazybone.entity.DelType;
import com.lin.lazybone.entity.Order;
import com.lin.lazybone.utils.L;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mr.lin on 2017/10/26.
 */

public class OrderListAdapter extends BaseAdapter implements View.OnClickListener
{
    private List<Order> mList;
    private Context mContext;
    private Callback mCallback;
    int type; //type为0保留上部按钮，为1保留下部按钮
    //保存小图标的id
    private Map<String, Integer> mDelTypeMap;

    public OrderListAdapter(Context mContext, List<Order> mList, Callback callback, int type)
    {
        this.mContext = mContext;
        this.mList = mList;
        mCallback = callback;
        this.type = type;
        //初始化快递信息
        mDelTypeMap = new HashMap<>();
        mDelTypeMap.put("顺丰", R.drawable.sf_mini);
        mDelTypeMap.put("申通", R.drawable.sto_mini);
        mDelTypeMap.put("中通", R.drawable.zto_mini);
        mDelTypeMap.put("ems", R.drawable.ems_mini);
        mDelTypeMap.put("百世汇通", R.drawable.best_mini);
        mDelTypeMap.put("圆通", R.drawable.yt_mini);
        mDelTypeMap.put("韵达", R.drawable.yunda_mini);
    }

    /**
     * 自定义接口回调点击时间
     */
    public interface Callback
    {
        public void buttonclick(View v);
    }


    @Override
    public int getCount()
    {
        return mList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder viewHolder = null;
        if (convertView == null)
        {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.orderlist_item, null);
            viewHolder.orderlist_button_cancel = (Button) convertView.findViewById(R.id.orderlist_button_cancel);
            viewHolder.orderlist_item_image = (ImageView) convertView.findViewById(R.id.orderlist_item_image);
            viewHolder.orderlist_item_layout = (LinearLayout) convertView.findViewById(R.id.orderlist_item_layout);
            viewHolder.orderlist_item_price = (TextView) convertView.findViewById(R.id.orderlist_item_price);
            viewHolder.orderlist_item_title = (TextView) convertView.findViewById(R.id.orderlist_item_title);
            viewHolder.orderlist_deltype = (TextView) convertView.findViewById(R.id.orderlist_deltype);
            viewHolder.orderlist_button_qiang = (Button) convertView.findViewById(R.id.orderlist_button_qiang);
            viewHolder.order_List_linearlayout = (LinearLayout) convertView.findViewById(R.id.order_List_linearlayout);
            viewHolder.orderlist_item_content = (TextView) convertView.findViewById(R.id.orderlist_item_content);
            viewHolder.orderlist_item_time = (TextView) convertView.findViewById(R.id.orderlist_item_time);
            viewHolder.orderlist_delstate = (TextView) convertView.findViewById(R.id.orderlist_delstate);
            convertView.setTag(viewHolder);
        } else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //设置数据
        Order order = mList.get(position);
        //设置需求类型
        viewHolder.orderlist_deltype.setText(order.getOrder_type() ? "取快递" : "寄快递");
        //设置图片
        viewHolder.orderlist_item_image.setBackgroundResource(mDelTypeMap.get(order.getOrder_del_type()));
        //设置标题
        viewHolder.orderlist_item_title.setText(order.getOrder_del_type());
        //设置价格
        viewHolder.orderlist_item_price.setText("￥ " + order.getOrder_price() + " RMB");
        //设置大概内容
        viewHolder.orderlist_item_content.setText(order.getOrder_from() + "---->" + order.getOrder_to());
        //设置时间
        viewHolder.orderlist_item_time.setText(order.getCreatedAt());
        //设置订单状态
        if (order.getOrder_state() == 1)
        {
            viewHolder.orderlist_delstate.setText("待接单");
        }else if (order.getOrder_state() == 2)
        {
            viewHolder.orderlist_delstate.setText("待送达");
        }else
        {
            viewHolder.orderlist_delstate.setText("已送达");
        }
        //给布局设置点击事件
        viewHolder.orderlist_button_cancel.setOnClickListener(this);
        viewHolder.orderlist_button_cancel.setTag(position);
        viewHolder.orderlist_item_layout.setOnClickListener(this);
        viewHolder.orderlist_item_layout.setTag(position);
        if (type == 0)
        {
            viewHolder.orderlist_button_qiang.setVisibility(View.GONE);
        } else
        {
            viewHolder.order_List_linearlayout.setVisibility(View.GONE);
            viewHolder.orderlist_button_qiang.setOnClickListener(this);
            viewHolder.orderlist_button_qiang.setTag(position);
        }
        //如果订单已完成，去掉取消订单按钮
        viewHolder.orderlist_button_cancel.setVisibility(order.getOrder_state() == 3 ? View.INVISIBLE : View.VISIBLE);
        return convertView;
    }

    class ViewHolder
    {

        TextView orderlist_deltype;
        ImageView orderlist_item_image;
        LinearLayout orderlist_item_layout;
        LinearLayout order_List_linearlayout;
        TextView orderlist_item_title;
        TextView orderlist_item_price;
        Button orderlist_button_cancel;
        Button orderlist_button_qiang;
        TextView orderlist_item_content;
        TextView orderlist_item_time;
        TextView orderlist_delstate;
    }

    @Override
    public void onClick(View v)
    {
        mCallback.buttonclick(v);
    }

}
